﻿package com.sz.ticket.servlet;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.ModelAndView;
import com.sz.ticket.bmo.ITicketBMO;

import org.springframework.web.servlet.mvc.Controller;

public class AddTicket extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
	protected final Log logger = LogFactory.getLog(getClass());
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}
	
	@SuppressWarnings("unchecked")
	public void doPost(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		try {
			
			String rtn_type = request.getParameter("return_type");
			String path = request.getContextPath(); 
			
			ServletContext servletContext = request.getSession().getServletContext();        
			ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
			
			ITicketBMO ticketBMO = (ITicketBMO) context.getBean("TicketBMO");

			Map map = new HashMap();
			
			String cust_tel 		= request.getParameter("cust_tel");
			String cust_name		= request.getParameter("cust_name");
			String business_id 		= request.getParameter("business_id");
			String demand_time_d 	= request.getParameter("demand_time_d");
			String demand_time_t 	= request.getParameter("demand_time_t");
			String demand_num 		= request.getParameter("demand_num");
			String demand_addition 	= request.getParameter("demand_addition");
			String staff_num 		= request.getParameter("staff_num");
			
			String email			= request.getParameter("email");
			String card_no			= request.getParameter("card_no");
			String cust_sex			= request.getParameter("cust_sex");
			String position_flag	= request.getParameter("position_flag");
			String window_flag		= request.getParameter("window_flag");
			String nosmoking_flag	= request.getParameter("nosmoking_flag");
			String baby_flag		= request.getParameter("baby_flag");
			String wc_flag			= request.getParameter("wc_flag");
			String notice_flag		= request.getParameter("notice_flag");
			String hurry_flag		= request.getParameter("hurry_flag");			
			
			String set_lockmanager_null = request.getParameter("set_lockmanager_null");
			
			String create_manager = request.getSession().getAttribute("userid").toString();
			String region_id = request.getSession().getAttribute("regionid").toString();	
			
			
			map.put("STATUS", "1");  //新增的订单状态就为1： 客户下单,待商家确认
			map.put("CUST_TEL", cust_tel);
			map.put("CUST_NAME", cust_name);
			map.put("BUSINESS_ID", business_id);
			map.put("DEMAND_TIME", String.format("%s %s", demand_time_d, demand_time_t));
			map.put("DEMAND_NUM", demand_num);
			map.put("DEMAND_ADDITION", demand_addition);
			map.put("STAFF_NUM", staff_num);
			map.put("REGION_ID", region_id);		
			map.put("CREATE_MANAGER", create_manager);
			map.put("OPERATOR", create_manager);
			
			map.put("EMAIL", email);
			map.put("CARD_NO", card_no);
			map.put("CUST_SEX", cust_sex);
			map.put("POSITION_FLAG", position_flag);
			map.put("WINDOW_FLAG", window_flag);
			map.put("NOSMOKING_FLAG", nosmoking_flag);
			map.put("BABY_FLAG", baby_flag);
			map.put("WC_FLAG", wc_flag);
			map.put("NOTICE_FLAG", notice_flag);
			map.put("HURRY_FLAG", hurry_flag);			
			
			if(set_lockmanager_null.equals("")) {
				map.put("LOCK_MANAGER", create_manager); //增加自动锁定
			}
			
			long r = ticketBMO.addTicket(map);
			logger.info(String.format("addTicket返回:%d", r));			

			if(r > 0) {
				if(rtn_type != null && rtn_type.compareToIgnoreCase("message")==0){
					response.setCharacterEncoding("utf-8");
					response.getWriter().print("操作成功");
				} else {
					response.sendRedirect(path + "/page/success.jsp");
				}
			} else {
				if(rtn_type != null && rtn_type.compareToIgnoreCase("message")==0){
					response.setCharacterEncoding("utf-8");
					response.getWriter().print("操作失败");
				} else {
					response.sendRedirect(path + "/page/failure.jsp");

				}
			}
		} catch (Exception e) {
			logger.error(e.getMessage());
			throw new ServletException(e.getMessage());
		}
	}
}
